<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="210"
			label-align="left">
			<u-form-item label="委外退货单别" v-if="judgeShow('tk001')">
				<u-input v-model="dataForm.tk001" disabled />
			</u-form-item>
			<u-form-item label="委外退货单号" v-if="judgeShow('tk001')">
				<u-input v-model="dataForm.tk002" disabled />
			</u-form-item>
			<u-form-item label="工厂" v-if="judgeShow('tk005')">
				<u-input v-model="dataForm.tk005" disabled />
			</u-form-item>
			<u-form-item label="打印" v-if="judgeShow('tk018')">
				<u-input v-model="dataForm.tk018" disabled />
			</u-form-item>
			<u-form-item label="单据日期" v-if="judgeShow('tk027')">
				<u-input v-model="dataForm.tk027" disabled />
			</u-form-item>
			<u-form-item label="委外退货日期" v-if="judgeShow('tk003')">
				<u-input v-model="dataForm.tk003" disabled />
			</u-form-item>
			<u-form-item label="审核者" v-if="judgeShow('tk028')">
				<u-input v-model="dataForm.tk028" disabled />
			</u-form-item>
			<u-form-item label="委外供应商" v-if="judgeShow('tk004')">
				<u-input v-model="dataForm.tk004" disabled />
			</u-form-item>
			<u-form-item label="签核状态" v-if="judgeShow('tk035')">
				<u-input v-model="dataForm.tk035" disabled />
			</u-form-item>
			<u-form-item label="部门" v-if="judgeShow('tk037')">
				<u-input v-model="dataForm.tk037" disabled />
			</u-form-item>
			<u-form-item label="项目编号" v-if="judgeShow('tk044')">
				<u-input v-model="dataForm.tk044" disabled />
			</u-form-item>
			<u-form-item label="生成分录成本" v-if="judgeShow('tk024')">
				<u-input v-model="dataForm.tk024" disabled />
			</u-form-item>
			<u-form-item label="生成分录费用" v-if="judgeShow('tk025')">
				<u-input v-model="dataForm.tk025" disabled />
			</u-form-item>
			<u-form-item label="附件">
				<jnpf-file v-model="dataForm.attachmentUpload" />
			</u-form-item>
			<u-tabs-swiper ref="tabs" :list="swiperList" :current="current" @change="tabsChange"
				:is-scroll="false"></u-tabs-swiper>
			<view class="jnpf-table" style="margin-top: 24rpx;" v-show="current==0">
				<u-form-item label="发票种类" v-if="judgeShow('tk011')">
					<u-input v-model="dataForm.tk011" disabled />
				</u-form-item>
				<u-form-item label="发票号码" v-if="judgeShow('tk013')">
					<u-input v-model="dataForm.tk013" disabled />
				</u-form-item>
				<u-form-item label="发票日期" v-if="judgeShow('tk012')">
					<u-input v-model="dataForm.tk012" disabled />
				</u-form-item>
				<u-form-item label="税号" v-if="judgeShow('tk010')">
					<u-input v-model="dataForm.tk010" disabled />
				</u-form-item>
			</view>
			<view class="jnpf-table" style="margin-top: 24rpx;" v-show="current==1">
				<u-form-item label="币种" v-if="judgeShow('tk006')">
					<u-input v-model="dataForm.tk006" disabled />
				</u-form-item>
				<u-form-item label="汇率" v-if="judgeShow('tk007')">
					<u-input v-model="dataForm.tk007" disabled />
				</u-form-item>
				<u-form-item label="件数" v-if="judgeShow('tk008')">
					<u-input v-model="dataForm.tk008" disabled />
				</u-form-item>
				<u-form-item label="税种" v-if="judgeShow('tk014')">
					<u-input v-model="dataForm.tk014" disabled />
				</u-form-item>
				<u-form-item label="税率" v-if="judgeShow('tk029')">
					<u-input v-model="dataForm.tk029" disabled />
				</u-form-item>
				<u-form-item label="自动扣料" v-if="judgeShow('tk023')">
					<u-input v-model="dataForm.tk023" disabled />
				</u-form-item>
				<u-form-item label="备注" v-if="judgeShow('tk009')">
					<u-input v-model="dataForm.tk009" />
				</u-form-item>
				<u-form-item label="付款条件编号" v-if="judgeShow('tk032')">
					<u-input v-model="dataForm.tk032" disabled />
				</u-form-item>
				<u-form-item label="厂供料自动扣料" v-if="judgeShow('tk034')">
					<u-input v-model="dataForm.tk034" disabled />
				</u-form-item>
			</view>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx;" v-for="(item,i) in dataForm.moctlList"
					:key="i">
					<u-form-item label="序号" style="background-color: aqua;">
						{{i+1}}
					</u-form-item>
					<u-form-item label="品号" v-if="judgeShow('moctlList-tl004')">
						<u-input v-model="item.tl004" disabled />
					</u-form-item>
					<u-form-item label="品名" v-if="judgeShow('moctlList-tl005')">
						<u-input v-model="item.tl005" disabled />
					</u-form-item>
					<u-form-item label="规格" v-if="judgeShow('moctlList-tl006')">
						<u-input v-model="item.tl006" disabled />
					</u-form-item>
					<u-form-item label="退货数量" v-if="judgeShow('moctlList-tl007')">
						<u-input v-model="item.tl007" disabled />
					</u-form-item>
					<u-form-item label="单位" v-if="judgeShow('moctlList-tl008')">
						<u-input v-model="item.tl008" disabled />
					</u-form-item>
					<u-form-item label="工艺" v-if="judgeShow('moctlList-tl017')">
						<u-input v-model="item.tl017" disabled />
					</u-form-item>
					<u-form-item label="仓库" v-if="judgeShow('moctlList-tl013')">
						<u-input v-model="item.tl013" disabled />
					</u-form-item>
					<u-form-item label="批号" v-if="judgeShow('moctlList-tl014')">
						<u-input v-model="item.tl014" disabled />
					</u-form-item>
					<u-form-item label="批号说明" v-if="judgeShow('moctlList-tl039')">
						<u-input v-model="item.tl039" disabled />
					</u-form-item>
					<u-form-item label="生产日期" v-if="judgeShow('moctlList-tl039')">
						<u-input v-model="item.tl039" disabled />
					</u-form-item>
					<u-form-item label="有效日期" v-if="judgeShow('moctlList-tl037')">
						<u-input v-model="item.tl037" disabled />
					</u-form-item>
					<u-form-item label="复检日期" v-if="judgeShow('moctlList-tl038')">
						<u-input v-model="item.tl038" disabled />
					</u-form-item>
					<u-form-item label="工单单别" v-if="judgeShow('moctlList-tl015')">
						<u-input v-model="item.tl015" disabled />
					</u-form-item>
					<u-form-item label="工单单号" v-if="judgeShow('moctlList-tl016')">
						<u-input v-model="item.tl016" disabled />
					</u-form-item>
					<u-form-item label="计价数量" v-if="judgeShow('moctlList-tl009')">
						<u-input v-model="item.tl009" disabled />
					</u-form-item>
					<u-form-item label="计价单位" v-if="judgeShow('moctlList-tl010')">
						<u-input v-model="item.tl010" disabled />
					</u-form-item>
					<u-form-item label="委外单价" v-if="judgeShow('moctlList-tl011')">
						<u-input v-model="item.tl011" disabled />
					</u-form-item>
					<u-form-item label="加工金额" v-if="judgeShow('moctlList-tl012')">
						<u-input v-model="item.tl012" disabled />
					</u-form-item>
					<u-form-item label="原币税前金额" v-if="judgeShow('moctlList-tl012')">
						<u-input v-model="item.tl012" disabled />
					</u-form-item>
					<u-form-item label="本币税前金额" v-if="judgeShow('moctlList-tl031')">
						<u-input v-model="item.tl031" disabled />
					</u-form-item>
					<u-form-item label="本币税额" v-if="judgeShow('moctlList-tl032')">
						<u-input v-model="item.tl032" disabled />
					</u-form-item>
					<u-form-item label="项目编号" v-if="judgeShow('moctlList-tl018')">
						<u-input v-model="item.tl018" disabled />
					</u-form-item>
					<u-form-item label="备注" v-if="judgeShow('moctlList-tl023')">
						<u-input v-model="item.tl023" disabled />
					</u-form-item>
					<u-form-item label="暂估码" v-if="judgeShow('moctlList-tl049')">
						<u-input v-model="item.tl049" disabled />
					</u-form-item>
					<u-form-item label="开票码" v-if="judgeShow('moctlList-tl025')">
						<u-input v-model="item.tl025" disabled />
					</u-form-item>
					<u-form-item label="已开票数量" v-if="judgeShow('moctlList-tl035')">
						<u-input v-model="item.tl035" disabled />
					</u-form-item>
					<u-form-item label="委外进货单别" v-if="judgeShow('moctlList-tl026')">
						<u-input v-model="item.tl026" disabled />
					</u-form-item>
					<u-form-item label="委外进货单号" v-if="judgeShow('moctlList-tl027')">
						<u-input v-model="item.tl027" disabled />
					</u-form-item>
					<u-form-item label="委外进货序号" v-if="judgeShow('moctlList-tl028')">
						<u-input v-model="item.tl028" disabled />
					</u-form-item>
					<u-form-item label="扣款工单别" v-if="judgeShow('moctlList-udf01')">
						<u-input v-model="item.udf01" disabled />
					</u-form-item>
					<u-form-item label="扣款工单号" v-if="judgeShow('moctlList-udf02')">
						<u-input v-model="item.udf02" disabled />
					</u-form-item>
					<u-form-item label="抵扣重量(G)或数量" v-if="judgeShow('moctlList-udf51')">
						<u-input v-model="item.udf51" disabled />
					</u-form-item>
					<u-form-item label="包材品号" v-if="judgeShow('moctlList-udf03')">
						<u-input v-model="item.udf03" disabled />
					</u-form-item>
					<u-form-item label="包材品名" v-if="judgeShow('moctlList-udf04')">
						<u-input v-model="item.udf04" disabled />
					</u-form-item>
					<u-form-item label="包材数量" v-if="judgeShow('moctlList-udf52')">
						<u-input v-model="item.udf52" disabled />
					</u-form-item>
				</view>
			</view>
			<u-form-item label="数量合计">
				<u-input v-model="dataForm.tk020" disabled />
			</u-form-item>
			<u-form-item label="本币税前金额">
				<u-input v-model="dataForm.tk030" disabled />
			</u-form-item>
			<u-form-item label="本币税额">
				<u-input v-model="dataForm.tk031" disabled />
			</u-form-item>
			<u-form-item label="原币税前金额">
				<u-input v-model="dataForm.tk017" disabled />
			</u-form-item>
			<u-form-item label="原币税额">
				<u-input v-model="dataForm.tk019" disabled />
			</u-form-item>
			<u-form-item label="本币金额合计">
				<u-input v-model="dataForm.Totalamountnlocalcurrency" disabled />
			</u-form-item>
			<u-form-item label="原币金额合计">
				<u-input v-model="dataForm.Totalamountinoriginalcurrency" disabled />
			</u-form-item>
		</u-form>
	</view>
</template>

<script>
	import comMixin from '../mixin'
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update,
	} from '@/api/apply/order'
	import {
		getMoctk,
	} from '@/api/workFlow/flowEngine'
	import {
		getDictionaryDataSelector,
		getDataInterfaceDataInfo
	} from '@/api/common'
	export default {
		mixins: [comMixin],
		computed: {},
		watch: {},
		data() {
			return {
				swiperList: [{
					name: '发票信息'
				}, {
					name: '其他信息'
				}],
				current: 0,
				show: false,

				dataForm: {},
				id: '',
				showForm: [],
				formOperatesList: [],
				options: [{
						fullName: "必选，唯一",
						id: "1"
					},
					{
						fullName: "必选，至少一个",
						id: "2"
					},
					{
						fullName: "可选，唯一",
						id: "3"
					},
					{
						fullName: "任意选",
						id: "4"
					},
				],
			}
		},
		methods: {
			tabsChange(e) {
				this.current = e
			},
			init(data, id, flowFormContentJson) {
				console.log('data', data);
				this.setting = data
				this.formOperatesList = data.formOperates
				this.id = id
				this.getInfoFn(data)
			},
			getInfoFn(data) {
				var list = data.id.split("-");
				let obj = {
					tk001: list[1],
					tk002: list[2]
				}
				getMoctk(obj).then(res => {
					if (res.code == 200) {
						this.dataForm = res.data;

						this.getDataInterfaceDataInfotk004(this.dataForm.tk004)
						this.getDataInterfaceDataInfotk005(this.dataForm.tk005)
						this.getDataInterfaceDataInfotk037(this.dataForm.tk037)
						this.getDataInterfaceDataInfotk006(this.dataForm.tk006)
						this.getDictionaryDatatk035()
						this.getDictionaryDatatk011()
						this.getDictionaryDatatk014()
						this.calculateAmount();
						if (this.dataForm.moctlList.length > 0) {
							this.getDataInterfaceDataInfock();
						}

					}
				})
			},
			//计算本币原币金额
			calculateAmount() {
				var totalQuantity = 0; //数量合计
				var localCurrencypretax = 0; //本币税前金额
				var localCurrencyTaxAmount = 0; //本币税额
				var originalCurrencypretax = 0; //原币税前金额
				var taxAmountinOriginalCurrency = 0; //原币税额
				var totalLocalCurrency = 0; //本币金额合计
				var totalOriginalCurrency = 0; //原币金额合计
				var moctlList = this.dataForm.moctlList;
				for (let i = 0; i < moctlList.length; i++) {
					totalQuantity += parseFloat(moctlList[i].tl007);
					localCurrencypretax += parseFloat(moctlList[i].tl031);
					localCurrencyTaxAmount += parseFloat(moctlList[i].tl032);
					originalCurrencypretax += parseFloat(moctlList[i].tl029);
					taxAmountinOriginalCurrency += parseFloat(moctlList[i].tl030);
					totalLocalCurrency +=
						parseFloat(moctlList[i].tl031) + parseFloat(moctlList[i].tl032);
					totalOriginalCurrency +=
						parseFloat(moctlList[i].tl029) + parseFloat(moctlList[i].tl030);
				}
				this.dataForm.tk020 = totalQuantity; //数量合计
				this.dataForm.tk030 = localCurrencypretax; //本币税前金额
				this.dataForm.tk031 = localCurrencyTaxAmount; //本币税额
				this.dataForm.tk017 = originalCurrencypretax; //原币税前金额
				this.dataForm.tk019 = taxAmountinOriginalCurrency; //原币税额
				this.dataForm.Totalamountnlocalcurrency = totalLocalCurrency; //本币金额合计
				this.dataForm.Totalamountinoriginalcurrency = totalOriginalCurrency; //原币金额合计
			},

			// 	委外供应商
			getDataInterfaceDataInfotk004(id) {
				let data = {
					id,
					interfaceId: '381247065990113925',
					propsValue: 'MA001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('381247065990113925', data).then(res => {
					this.dataForm.tk004 = res.data.JoinStr
				})
			},
			// 	工厂名称
			getDataInterfaceDataInfotk005(id) {
				let data = {
					id,
					interfaceId: '362111000289221189',
					propsValue: 'MB001',
					relationField: 'JoinStr',
				}
				console.log('部门编号data', data);
				getDataInterfaceDataInfo('362111000289221189', data).then(res => {
					this.dataForm.tk005 = res.data.JoinStr
				})
			},
			// 	部门
			getDataInterfaceDataInfotk037(id) {
				let data = {
					id,
					interfaceId: '381240499823979141',
					propsValue: 'ME001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('381240499823979141', data).then(res => {
					this.dataForm.tk037 = res.data.JoinStr
				})
			},
			// 	币种
			getDataInterfaceDataInfotk006(id) {
				let data = {
					id,
					interfaceId: '362115609221143109',
					propsValue: 'MF001',
					relationField: 'MF002',
				}
				getDataInterfaceDataInfo('362115609221143109', data).then(res => {
					this.dataForm.tk006 = res.data.JoinStr
				})
			},
			// 	签核状态
			getDictionaryDatatk035() {
				getDictionaryDataSelector('393566623006470277').then(res => {
					const matchedItem = res.data.list.filter(item => item.enCode == this.dataForm.tk035)[
						0];
					this.dataForm.tk035 = matchedItem ? matchedItem.fullName : null;
				})
			},
			// 	发票种类
			getDictionaryDatatk011() {
				getDictionaryDataSelector('382030349477096325').then(res => {
					const matchedItem = res.data.list.filter(item => item.enCode == this.dataForm.tk011)[
						0];
					this.dataForm.tk011 = matchedItem ? matchedItem.fullName : null;
				})
			},
			// 	税种
			getDictionaryDatatk014() {
				getDictionaryDataSelector('411695330707391429').then(res => {
					const matchedItem = res.data.list.filter(item => item.enCode == this.dataForm.tk014)[
						0];
					this.dataForm.tk014 = matchedItem ? matchedItem.enCode + ':' +
						matchedItem.description : null;
				})
			},
			getDataInterfaceDataInfock() {
				let list = this.dataForm.moctlList
				for (let i = 0; i < list.length; i++) {
					let data = {
						id: list[i].tl013,
						interfaceId: '362114335423601221',
						propsValue: 'MC001',
						relationField: 'MC001',
					}
					getDataInterfaceDataInfo('362114335423601221', data).then(res => {
						this.dataForm.moctlList[i].tl013 = res.data.MC002
					})
				}

			},
			selfGetInfo() {
				Info(this.setting.id).then(res => {
					this.dataForm = res.data
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson)
					}
				})
			},
			selfSubmit() {
				this.dataForm.status = this.eventType === 'submit' ? 0 : 1
				if (this.eventType === 'save') return this.selfHandleRequest()
				uni.showModal({
					title: '提示',
					content: '您确定要提交当前流程吗？',
					success: res => {
						if (res.confirm) {
							this.selfHandleRequest()
						}
					}
				})
			},
			selfHandleRequest() {
				if (!this.dataForm.id) delete(this.dataForm.id)
				const formMethod = this.dataForm.id ? Update : Create
				formMethod(this.dataForm).then(res => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit('refresh')
								uni.navigateBack()
							}, 1500)
						}
					})
				})
			},
			initData() {
				getGoodsList().then(res => {
					this.goodsOptions = res.data.list
				})
				getCustomerList().then(res => {
					this.customerOptions = res.data.list
				})
			},
			selfInit() {
				this.addGoods()
				this.addPlan()
			},
			onChange(e) {
				const user = e[0]
				this.dataForm.salesmanName = user.fullName
			},
			addGoods() {
				const item = {
					goodsId: '',
					goodsCode: '',
					goodsName: '',
					specifications: '',
					unit: '',
					qty: '',
					price: '',
					amount: '',
					discount: null,
					cess: null,
					actualPrice: '',
					actualAmount: '',
					description: ''
				}
				this.dataForm.goodsList.push(item)
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1)
			},
			addPlan() {
				const item = {
					receivableDate: '',
					receivableRate: '',
					receivableMoney: '',
					receivableMode: '',
					abstract: ''
				}
				this.dataForm.collectionPlanList.push(item)
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1)
			},
			onGoodsChange(e, i) {
				const goods = e[0]
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: ''
				}
				this.$forceUpdate()
			},
			onCustomerChange(e) {
				const customer = e[0]
				this.dataForm.customerName = customer.text
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(parseFloat(row.price) * parseFloat(row.qty))
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(discountPrice * (1 + (row.cess / 100)));
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(parseFloat(row.actualPrice) * parseFloat(row
					.qty))
				this.$forceUpdate()
			}
		}
	}
</script>